﻿using Microsoft.International.Converters.PinYinConverter;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace TestCode
{
    class Program
    {
        static void Main(string[] args)
        {
            //string str = "abc张三李四";
            //foreach(var ch in str)
            //{
            //    Console.WriteLine($"ch:{ch}");
            //    Console.WriteLine($"=== {Convert.ToInt32(ch).ToString("x2")}");
            //}
            //Console.WriteLine("==================将unicode字符转换成char=====================");
            //char ch2 = Convert.ToChar(int.Parse("5f20", System.Globalization.NumberStyles.HexNumber));
            //Console.WriteLine($"ch2 is {ch2}");
            //Console.WriteLine($"{Convert.ToInt32('张').ToString("x2")}");
            //Console.WriteLine($"{Convert.ToChar(int.Parse("5f20", System.Globalization.NumberStyles.HexNumber))}");

            // 获取汉字的拼音
            //string str = "我AI你中国!123";
            //StringBuilder sb = new StringBuilder();
            //foreach(var ch in str)
            //{
            //    if (isChineseChar(ch.ToString()))
            //    {
            //        ChineseChar cc = new ChineseChar(ch);
            //        if(cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
            //        {
            //            sb.Append(cc.Pinyins[0].ToString());
            //        }
            //    }
            //    else
            //    {
            //        sb.Append(ch);
            //    }
            //}
            //Console.WriteLine(sb.ToString());
            //Console.WriteLine($"newline:{Environment.NewLine}");

            //string result = string.Empty;
            //foreach (char item in str)
            //{
            //    try
            //    {
            //        
            //        if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
            //        {
            //            string temp = cc.Pinyins[0].ToString();
            //            result += temp.Substring(0, temp.Length - 1);
            //        }
            //    }
            //    catch (Exception)
            //    {
            //        result += item.ToString();
            //    }
            //}
            //Console.WriteLine(result);//"WOAINIZHONGGUO!123"
            //Console.WriteLine($"{isChineseChar("张")}");
            //Console.WriteLine($"{isChineseChar("张三")}");
            //Console.WriteLine($"{isChineseChar("张三1")}");
            //Console.WriteLine($"{isChineseChar("张1三")}");

            //string str = " C #   变   成 死  \t 像  ";
            //string result = RemoveBlank(str);
            //Console.WriteLine(result);
            //Console.WriteLine(result.Length);

            //string str = "Hello world! My name is zhangle";
            ////string newStr = str.Replace('e', 'E');
            //string newStr = str.Replace("e", "EEEEE");
            //Console.WriteLine(newStr);

            Console.ReadKey();
        }

        public static string encode(string text) {
            MemoryStream ms = new MemoryStream();

            CryptoStream cs = new CryptoStream(
                ms,
                new DESCryptoServiceProvider().CreateEncryptor(),
                CryptoStreamMode.Write // 对加密流进行写访问
                );

            byte[] byteArr = Encoding.UTF8.GetBytes(text);
            cs.Write(byteArr, 0, byteArr.Length);
            cs.FlushFinalBlock(); // 将数据压入基础流

            //  得到加密信息的字节数组
            byte[] tmpBuffer = ms.ToArray();
            string result = Convert.ToBase64String(tmpBuffer);
            Console.WriteLine($"result:{result}");
            return result;
        }


        public static string RemoveBlank(string text)
        {
            char[] charArr = text.ToCharArray();
            IEnumerator chrEnum = charArr.GetEnumerator();
            StringBuilder sb = new StringBuilder();
            while (chrEnum.MoveNext())
            {
                char ch = (char)chrEnum.Current;
                if(ch == ' ' ||  ch == '\t')
                {

                }
                else
                {
                    sb.Append(ch);
                }
            }
            return sb.ToString();
        }

        public static bool isChineseChar(string chStr) {
            Regex regex = new Regex("^[\u4e00-\u9fa5]$");
            return regex.IsMatch(chStr);
        }
    }
}
